package com.itheima.controllor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import service.CheckItemsService;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/checkItems")
public class CheckItemsControllor {

    @Reference
    private CheckItemsService checkItemsService;

    @Autowired
    private JedisPool jedisPool;


    /**
     * 新增检查项目
     * @param checkItem
     * @return
     */

    @RequestMapping("/addItems")
    public Result addItems( @RequestBody CheckItem checkItem){
        try {
            checkItemsService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }


    /**
     * 检查项目根据 查询框 条件来查询
     * @param queryPageBean
     * @return
     */
    //@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findItemsByCondition")
    public PageResult addItems( @RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=null;
        try {
            pageResult= checkItemsService.findItemsByCondition(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pageResult;
    }

    //删除
    //@PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteItemsById")
    public Result deleteItemsById( int id){


        try {
            checkItemsService.deleteItemsById(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);

        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());

        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }


    }
    @RequestMapping("/findItemsById")
    public CheckItem findItemsById(int id){

        try {
            CheckItem checkItem=checkItemsService.findItemsById(id);
            return checkItem;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }


    //编辑
    @RequestMapping("/updateItems")
    public Result updateItems(@RequestBody CheckItem checkItem){

        try {
            checkItemsService.updateItems(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }


    }
    ////查询所有的检查组
    @RequestMapping("/findAll")
    public List<CheckItem> findAll(){

        try {
            List<CheckItem> itemList=checkItemsService.findAll();
            return itemList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }









}
